/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_auth_check.js
*  Description  : 핸드폰인증
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-08-17  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	function clsAuthText()
	{
		this._HTMLDom = null;
		this._ERRMSG = {
			"NOVALUE": "인증번호를 입력해 주십시요."
		}
		this.errMsg = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsAuthText.prototype = {
		_init: function(){
			var $layer = $("dl.pop_auth");
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
		}
		, getInfo: function(){
			return this.infoData;
		}
		, getValue: function(){
			return this.$getLayer().find("input:text").val();
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer = this.$getLayer();
			
			if(!this.getValue()){
				this.errMsg = "NOVALUE";
				return false;
			}
			
			return true;
		}
		, focus: function(){
			this.$getLayer().find("input:text").trigger("focus");
		}
	}
	
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 인증번호 요청
		, authRequest: function(arg){
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "cpMobileStep1";
			var options = {
				"hpComp": arg["통신사"]
				, "hpNum": arg["휴대폰"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					res["결과값"] = {
						"응답코드": _util.nvl(obj["returnCode"])
						, "요청코드": _util.nvl(obj["requestSeq"])
						, "인증코드": _util.nvl(obj["responseSeq"])
						, "메시지": _util.nvl(obj["message"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		// 인증번호 검증
		, authValid: function(arg){
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "cpMobileStep2";
			var options = {
				"authNo": arg["인증번호"]
				, "requestSeq": arg["요청코드"]
				, "responseSeq": arg["인증코드"]
			}
			_gf.ajax({
				"sid": "US001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					res["결과값"] = {
						"응답코드": _util.nvl(obj["returnCode"])
						, "메시지": _util.nvl(obj["message"])
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objAuthText = null;
		
		this._permitStart = null;
		this.q_tele = null;
		this.q_hp = null;
		this.openerControl = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objAuthText = new clsAuthText();
			
			this._bind();
			this._beforeStart();
			
			_util.setWindowSize();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.q_tele = _util.getParam("q_tele");
			this.q_hp = _util.getParam("q_hp");
			
			if(opener && opener.document && opener.document.objControl) this.openerControl = opener.document.objControl;
			
			if(!this.q_tele || !this.q_hp || !this.openerControl) self.close();
		}
		, _bind: function(){
			var thisClass = this;
			
			this.objAuthText.$getLayer().on("click", "img[alt='확인']", function(){
				thisClass.authValid();
			});
			
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			// 인증코드요청
			this.objComm.authRequest({
				"통신사": this.q_tele
				, "휴대폰": this.q_hp.replace(/-/g, "")
				, "콜백": function(ret){
					thisClass.finishAuthRequest(ret);
				}
			});
		}
		// 인증코드요청완료
		, finishAuthRequest: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var d = ret["결과값"];
			
			if(d["응답코드"] == "0000"){
				alert("인증코드가 발송되었습니다.");
			}else{
				alert("인증코드 요청에 실패하였습니다.\n" + d["메시지"]);
				return;
			}
			
			this.objAuthText.setInfo(d);
		}
		// 인증요청 완료
		, finishAuthValid: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var d = ret["결과값"];
			
			if(d["응답코드"] != "0000"){
				alert(d["올바른 인증번호를 입력해 주십시요."]);
				return;
			}
			
			this.openerControl.returnHPCheck(self, this.q_tele, this.q_hp);
		}
		// 인증요청
		, authValid: function(){
			var thisClass = this;
			
			if(!this.objAuthText.valid()){
				alert(this.objAuthText.getError());
				this.objAuthText.focus();
				return;
			}
			
			var d = this.objAuthText.getInfo();
			
			if(!d){
				alert("인증정보가 발송되지 않았습니다");
				return;
			}
			
			this.objComm.authValid({
				"인증번호": this.objAuthText.getValue()
				, "요청코드": d["요청코드"]
				, "인증코드": d["인증코드"]
				, "콜백": function(ret){
					thisClass.finishAuthValid(ret);
				}
			});
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});